<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

use OT\DataDictionary;

header("Content-type: text/html; charset=utf-8");

/**
 * 营销系统控制器
 */
class MarketingController extends HomeController
{

	/**
	 * 检测是否登录
	 * */
	public function _initialize()
	{
		if (empty(session('user_id'))) {
			header('Location: ' . U('/Home/Login/Login'));
			exit;
		} else {
			//再来判断权限
			if (session('Supertube') == 1) {
				//超管 拥有所有权限
			} else {
				$id = session('user_id');//不是超管的时候 user_id是account_number的主键ID
				$jurisdiction = M('account_number')->where("id=$id")->getField('jurisdiction');
				$action_name = ACTION_NAME;
				if ($action_name == groupWork || $action_name == GroupWork) {
					//访问数据统计
					$domain = strstr($jurisdiction, ',19,');
					if (!$domain) {
						$this->error('没有访问权限');
					}
				} else if ($action_name == seckill || $action_name == Seckill) {
					//排行榜
					$domain = strstr($jurisdiction, ',20,');
					if (!$domain) {
						$this->error('没有访问权限');
					}
				} else if ($action_name == coupon || $action_name == Coupon) {
					//线索列表
					$domain = strstr($jurisdiction, ',21,');
					if (!$domain) {
						$this->error('没有访问权限');
					}
				}
			}
		}
	}

	/**
	 * Mr.Jiang
	 * 课程拼团列表
	 */
	public function groupWork()
	{
		$map['iid'] = session('iid');

		$_REQUEST['name'] ? $map['name'] = array('like', "%$_REQUEST[name]%") : '';
		$_REQUEST['status'] ? $map['status'] = $_REQUEST['status'] : '';
		if ($_REQUEST['start_time'] && $_REQUEST['end_time']) {
			$btime = strtotime($_REQUEST['start_time']);
			$etime = strtotime($_REQUEST['end_time'] . " 23:59:59");
			$map['create_time'] = array('between', "$btime,$etime");
		}

		$count = M('group_work')->count();
		$Page = new \Think\Page($count, 10, $_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		// $list = M('group_work')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
		//tzb 应该有条件 机构ID什么的
		$list = M('group_work')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->where($map)->select();
		foreach ($list as $k => $v) {
			$list[$k]['start_time'] = date('Y-m-d H:i:s', $v['start_time']);
			$list[$k]['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
			if ($v['status'] == '0') {
				$list[$k]['statuss'] = '未开始';
			} else if ($v['status'] == '1') {
				$list[$k]['statuss'] = '进行中';
			} else if ($v['status'] == '2') {
				$list[$k]['statuss'] = '已结束';
			} else {
				$list[$k]['status'] = '已完成';
			}
			$list[$k]['group_count'] = M('group_join')->where("gid=$v[id] && pid=0")->count();
		}
		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '课程拼团列表';
		$this->assign('msg', $_REQUEST);
		$this->assign('cur', '课程拼团');
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 新增拼团
	 */
	public function addGroupWork()
	{
		//tzb 把$_REQUEST改成了sesiion
		$iid = session('iid');
		if (IS_POST) {
			// print_r($_REQUEST);die;
			//tzb  加个机构ID
			$data['iid'] = $iid;

			$data['category'] = $_REQUEST['category'];
			$data['one_type_id'] = $_REQUEST['one_type_id'];
			$data['two_type_id'] = $_REQUEST['two_type_id'];
			$data['course_id'] = $_REQUEST['course_id'];
			$data['start_time'] = strtotime($_REQUEST['start_time']);
			$data['end_time'] = strtotime($_REQUEST['end_time']);
			$data['people_number'] = $_REQUEST['people_number'];
			$data['time_limit'] = $_REQUEST['time_limit'];
			$data['group_price'] = $_REQUEST['group_price'];
			$data['chief_price'] = $_REQUEST['chief_price'];
			$data['group_limit_number'] = $_REQUEST['group_limit_number'];

			$data['name'] = $_REQUEST['course_name'];
			if (empty($data['course_id'])) {
				$this->error("请选择课程");
			}

			if (empty($data['start_time'])) {
				$this->error("请选择拼团开始时间");
			}

			if (empty($data['end_time'])) {
				$this->error("请选择拼团结束时间");
			} else {
				if ($data['end_time'] < $data['start_time']) {
					$this->error("拼团结束时间不能小于拼团开始时间");
				} else {
					if ($data['end_time'] < time()) {
						$data['status'] = 2;
					} else {
						if ($data['start_time'] > time()) {
							$data['status'] = 0;
						} else {
							$data['status'] = 1;
						}
					}
				}
			}

			if ($data['people_number'] < 1) {
				$this->error("拼团人数至少一人");
			}

			if (empty($data['time_limit'])) {
				$this->error("请输入拼团时限");
			}

			if (empty($data['group_limit_number'])) {
				$this->error("请输入限制成团数");
			}

			$time = time();
			$data['create_time'] = $time;
			$data['update_time'] = $time;
			$result = D('group_work')->add($data);
			if ($result) {
				//新增成功之后，对应的课程ID is_acticity状态要变
				if ($data['start_time'] <= time()) {
					//开始时间大于等于当前时间，活动直接开始
					$data['status'] = 1;
					if ($data['category'] == 1) {
						//面授
						M('face_course')->where("id=$data[course_id]")->save(['is_activity' => 1]);
					} else if ($data['category'] == 2) {
						//点播
						M('video_course')->where("id=$data[course_id]")->save(['is_activity' => 1]);
					} else {
						//直播
						M('live_course')->where("id=$data[course_id]")->save(['is_activity' => 1]);
					}
				} else {
					//活动添加后未开始，添加redis集合，到了对应开始，结束时间就自动执行相关程序
					$redisCtr = A('Home/Redis');
					$task_param = array(
						'group_work_id' => $result,
						'category' => $data['category'],
						'course_id' => $data['course_id']
					);
					//添加开始执行的redis记录到集合
					$redisCtr->addRedis('groupStart', $data['start_time'], $task_param);
					//添加结束执行的redis记录到集合
					$redisCtr->addRedis('groupEnd', $data['end_time'], $task_param);
				}

				if (session('Supertube') == 2) {
					//不记录超管的 只记录这种有权限的
					$data['account_id'] = session('user_id');
					$data['create_time'] = time();
					$data['ip_address'] = $_SERVER["REMOTE_ADDR"];
					$data['content'] = '课程拼团' . '-' . '新增拼团';
					M('account_log')->add($data);
				}
				$this->success("新增成功", U('groupWork'));
			} else {
				$this->error("新增失败");
			}
		} else {
			//父级分类
			$parent = D('face_course_type')->where(array('pid' => 0))->order('id desc')->select();
			//子级分类
			$child = D('face_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
			//课程
			$course = D('face_course')->where(array('iid' => $iid, 'one_type_id' => $parent[0]['id'], 'two_type_id' => $child[0]['id'], 'is_del' => 0))->order('id desc')->select();
			foreach ($course as $k => $v) {
				$course[$k]['img'] = M('picture')->where("id=$v[img]")->getfield('path');
			}
			$this->assign('parent', $parent);
			$this->assign('child', $child);
			$this->assign('course', $course);
			$this->assign('cur', '课程拼团');
			$this->display();
		}
	}

	//tzb 拼团详情
	public function group_details()
	{
		$id = $_REQUEST['id'];
		$group_info = M('group_work')->where("id=$id")->find();
		if ($group_info['category'] == 1) {
			$group_info['category'] = '面授课';
			$group_info['original_price'] = M('face_course')->where("id=$group_info[course_id]")->getfield('price');
			$group_info['img'] = M('face_course')->where("id=$group_info[course_id]")->getfield('img');
		} else if ($group_info['category'] == 2) {
			$group_info['category'] = '点播课';
			$group_info['original_price'] = M('video_course')->where("id=$group_info[course_id]")->getfield('price');
			$group_info['img'] = M('video_course')->where("id=$group_info[course_id]")->getfield('img');
		} else {
			$group_info['category'] = '直播课';
			$group_info['original_price'] = M('live_course')->where("id=$group_info[course_id]")->getfield('price');
			$group_info['img'] = M('live_course')->where("id=$group_info[course_id]")->getfield('img');
		}
		// print_r($group_info['img']);die;
		if ($group_info['img']) {
			$group_info['img'] = M('picture')->where("id=$group_info[img]")->getField('path');
		} else {
			$group_info['img'] = '';
		}

		$group_info['start_time'] = date('Y-m-d', $group_info['start_time']);
		$group_info['end_time'] = date('Y-m-d', $group_info['end_time']);
		$count = M('group_join')->where("gid=$id && pid=0")->count();
		$join_list = M('group_join')->where("gid=$id && pid=0")->select();
		foreach ($join_list as $k => $v) {
			$join_list[$k]['sub'] = M('group_join a')->join('teach_ucenter_member b on a.uid=b.id')->where("a.pid=$v[id]")->select();
			$join_list[$k]['username'] = M('ucenter_member')->where("id=$v[uid]")->getfield('username');
		}
		// echo '<pre>';print_r($join_list);die;
		$this->assign('group_info', $group_info);
		$this->assign('join_list', $join_list);
		$this->assign('count', $count);
		$this->assign('cur', '课程拼团');
		$this->display();
	}

	//删除拼团
	public function group_del()
	{
		$id = $_REQUEST['id'];
		$res = M('group_work')->where("id=$id")->delete();
		if ($res) {
			if (session('Supertube') == 2) {
				//不记录超管的 只记录这种有权限的
				$data['account_id'] = session('user_id');
				$data['create_time'] = time();
				$data['ip_address'] = $_SERVER["REMOTE_ADDR"];
				$data['content'] = '课程拼团' . '-' . '删除拼团';
				M('account_log')->add($data);
			}
			M('group_join')->where("gid=$id")->delete();
			$this->success('成功');
		} else {
			$this->error('失败');
		}
	}

	/**
	 * Mr.Jiang
	 * 根据类别获得分类
	 */
	public function getType()
	{
		$category = $_REQUEST['category'];//1面授，2点播，3直播
		if ($category == 1) {
			$parent = D('face_course_type')->where(array('pid' => 0))->order('id desc')->select();
			$child = D('face_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
		} else if ($category == 2) {
			$parent = D('video_course_type')->where(array('pid' => 0))->order('id desc')->select();
			$child = D('video_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
		} else {
			$parent = D('live_course_type')->where(array('pid' => 0))->order('id desc')->select();
			$child = D('live_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
		}
		$data['parent'] = $parent ? $parent : [];
		$data['child'] = $child ? $child : [];

		return api_return(1, '信息获取成功', $data);
	}

	//优惠券有个书籍分类 tzb
	public function getTypes()
	{
		$category = $_REQUEST['category'];//1面授，2点播，3直播
		// print_r($category);die;
		if ($category == 1) {
			$parent = D('face_course_type')->where(array('pid' => 0))->order('id desc')->select();
			$child = D('face_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
		} else if ($category == 2) {
			$parent = D('video_course_type')->where(array('pid' => 0))->order('id desc')->select();
			$child = D('video_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
		} else if ($category == 3) {
			$parent = D('live_course_type')->where(array('pid' => 0))->order('id desc')->select();
			$child = D('live_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
		} else {
			$parent = D('goods_type')->where(array('pid' => 0))->order('id desc')->select();
			$child = D('goods_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
		}
		$data['parent'] = $parent ? $parent : [];
		$data['child'] = $child ? $child : [];

		return api_return(1, '信息获取成功', $data);
	}

	/**
	 * Mr.Jiang
	 * 根据类别与父类获得子类
	 */
	public function getChildType()
	{
		$category = $_REQUEST['category'];//1面授，2点播，3直播
		$pid = $_REQUEST['pid'];
		if ($category == 1) {
			$child = D('face_course_type')->where(array('pid' => $pid))->order('id desc')->select();
		} else if ($category == 2) {
			$child = D('video_course_type')->where(array('pid' => $pid))->order('id desc')->select();
		} else {
			$child = D('live_course_type')->where(array('pid' => $pid))->order('id desc')->select();
		}

		return api_return(1, '信息获取成功', $child);
	}

	//tzb 书籍分类
	public function getChildTypes()
	{
		$category = $_REQUEST['category'];//1面授，2点播，3直播
		$pid = $_REQUEST['pid'];
		if ($category == 1) {
			$child = D('face_course_type')->where(array('pid' => $pid))->order('id desc')->select();
		} else if ($category == 2) {
			$child = D('video_course_type')->where(array('pid' => $pid))->order('id desc')->select();
		} else if ($category == 3) {
			$child = D('live_course_type')->where(array('pid' => $pid))->order('id desc')->select();
		} else {
			$child = D('goods_type')->where(array('pid' => $pid))->order('id desc')->select();
		}

		return api_return(1, '信息获取成功', $child);
	}

	/**
	 * Mr.Jiang
	 * 根据类别与分类获取课程
	 */
	public function getCourse()
	{
		$category = $_REQUEST['category'];//1面授，2点播，3直播
		$type_id = $_REQUEST['type_id'];
		// $iid = $_REQUEST['iid'];//机构ID
		$iid = session('iid');
		//新加一个课程名称搜索
		$_REQUEST['name'] ? $name = array('like', "%$_REQUEST[name]%") : $name = '';
		if ($category == 1) {

			if ($name) {
				$course = D('face_course')->where(array('iid' => $iid, 'two_type_id' => $type_id, 'name' => $name, 'is_del' => 0))->order('id desc')->select();
			} else {
				$course = D('face_course')->where(array('iid' => $iid, 'two_type_id' => $type_id, 'is_del' => 0))->order('id desc')->select();
			}
		} else if ($category == 2) {
			if ($name) {
				$course = D('video_course')->where(array('iid' => $iid, 'two_type_id' => $type_id, 'name' => $name, 'is_del' => 0))->order('id desc')->select();
			} else {
				$course = D('video_course')->where(array('iid' => $iid, 'two_type_id' => $type_id, 'is_del' => 0))->order('id desc')->select();
			}
		} else {
			if ($name) {
				$course = D('live_course')->where(array('iid' => $iid, 'two_type_id' => $type_id, 'name' => $name, 'is_del' => 0))->order('id desc')->select();
			} else {
				$course = D('live_course')->where(array('iid' => $iid, 'two_type_id' => $type_id, 'is_del' => 0))->order('id desc')->select();
			}
		}

		//tzb 处理下图片
		foreach ($course as $k => $v) {
			$course[$k]['img'] = M('picture')->where("id=$v[img]")->getfield('path');
		}

		return api_return(1, '信息获取成功', $course);
	}

	//tzb  优惠券哪里选择商品多了书籍分类
	public function getCourses()
	{
		$category = $_REQUEST['category'];//1面授，2点播，3直播
		$type_id = $_REQUEST['type_id'];
		// $iid = $_REQUEST['iid'];//机构ID
		$iid = session('iid');
		//新加一个课程名称搜索
		$_REQUEST['name'] ? $name = array('like', "%$_REQUEST[name]%") : $name = '';
		if ($category == 1) {

			if ($name) {
				$course = D('face_course')->where(array('iid' => $iid, 'two_type_id' => $type_id, 'name' => $name, 'is_del' => 0))->order('id desc')->select();
			} else {
				$course = D('face_course')->where(array('iid' => $iid, 'two_type_id' => $type_id, 'is_del' => 0))->order('id desc')->select();
			}
		} else if ($category == 2) {
			if ($name) {
				$course = D('video_course')->where(array('iid' => $iid, 'two_type_id' => $type_id, 'name' => $name, 'is_del' => 0))->order('id desc')->select();
			} else {
				$course = D('video_course')->where(array('iid' => $iid, 'two_type_id' => $type_id, 'is_del' => 0))->order('id desc')->select();
			}
		} else if ($category == 3) {
			if ($name) {
				$course = D('live_course')->where(array('iid' => $iid, 'two_type_id' => $type_id, 'name' => $name, 'is_del' => 0))->order('id desc')->select();
			} else {
				$course = D('live_course')->where(array('iid' => $iid, 'two_type_id' => $type_id, 'is_del' => 0))->order('id desc')->select();
			}
		} else {

			if ($name) {
				$course = D('goods')->where(array('two_type_id' => $type_id, 'name' => $name, 'is_del' => 0))->order('id desc')->select();
			} else {

				$course = D('goods')->where(array('two_type_id' => $type_id, 'is_del' => 0))->order('id desc')->select();
				foreach ($course as $k => $v) {
					$imgg = explode(',', $v['imgs']);
					$course[$k]['img'] = $imgg[0];
				}
			}
		}

		//tzb 处理下图片
		foreach ($course as $k => $v) {
			$course[$k]['img'] = M('picture')->where("id=$v[img]")->getfield('path');
		}

		return api_return(1, '信息获取成功', $course);
	}

	//tzb 跟据分类跟课程ID得到详细课程数据，回显
	public function getCourseid()
	{
		$category = $_REQUEST['category'];//1面授，2点播，3直播
		$course_id = $_REQUEST['course_id'];

		if ($category == 1) {
			$course = D('face_course')->where(array('id' => $course_id))->find();
			$course['category'] = '面授';
		} else if ($category == 2) {
			$course = D('video_course')->where(array('id' => $course_id))->find();
			$course['category'] = '点播';
		} else {
			$course = D('live_course')->where(array('id' => $course_id))->find();
			$course['category'] = '直播';
		}
		//tzb 处理下图片
		if ($course['img']) {
			$course['img'] = M('picture')->where("id=$course[img]")->getfield('path');
		} else {
			$course['img'] = '';
		}

		return api_return(1, '信息获取成功', $course);
	}

	//tzb
	public function getCourselist()
	{
		$category = $_REQUEST['category'];//1面授，2点播，3直播
		$course_id = $_REQUEST['course_id'];
		// print_r($course_id);die;
		//         Array
		// (
		//     [0] => 2
		//     [1] => 1
		// )

		if ($category == 1) {
			foreach ($course_id as $k => $v) {

				// $course[$k]['id']=$v;
				$course[$k] = D('face_course')->where(array('id' => $v))->find();
				$course[$k]['category_name'] = '面授';
				$course[$k]['category'] = 1;
			}
		} else if ($category == 2) {
			foreach ($course_id as $k => $v) {
				// $course[$k]['category']=2;
				// $course[$k]['id']=$v;
				$course[$k] = D('video_course')->where(array('id' => $v))->find();
				$course[$k]['category_name'] = '点播';
				$course[$k]['category'] = 2;
			}
		} else if ($category == 3) {
			foreach ($course_id as $k => $v) {
				// $course[$k]['category']=3;
				// $course[$k]['id']=$v;
				$course[$k] = D('live_course')->where(array('id' => $v))->find();
				$course[$k]['category_name'] = '直播';
				$course[$k]['category'] = 3;
			}
		} else {
			foreach ($course_id as $k => $v) {
				// $course[$k]['category']=4;
				// $course[$k]['id']=$v;
				$course[$k] = D('goods')->where(array('id' => $v))->find();
				$imgg = explode(',', $course[$k]['imgs']);
				$course[$k]['img'] = $imgg[0];
				$course[$k]['category_name'] = '书籍';
				$course[$k]['category'] = 4;
			}
		}
		//tzb 处理下图片
		foreach ($course as $k => $v) {
			$course[$k]['img'] = M('picture')->where("id=$v[img]")->getfield('path');
		}

		return api_return(1, '信息获取成功', $course);
	}

	/**
	 * Mr.Jiang
	 * 课程秒杀列表
	 */
	public function seckill()
	{
		//tzb 搜索条件
		$map['iid'] = session('iid');
		$_REQUEST['name'] ? $map['name'] = array('like', "%$_REQUEST[name]%") : '';
		$_REQUEST['status'] ? $map['status'] = $_REQUEST['status'] : '';
		if ($_REQUEST['start_time'] && $_REQUEST['end_time']) {
			$btime = strtotime($_REQUEST['start_time']);
			$etime = strtotime($_REQUEST['end_time'] . " 23:59:59");
			$map['create_time'] = array('between', "$btime,$etime");
		}

		$count = M('seckill')->count();
		$Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('seckill')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->where($map)->select();
		//tzb
		foreach ($list as $k => $v) {
			$list[$k]['start_time'] = date('Y-m-d H:i:s', $v['start_time']);
			$list[$k]['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
			if ($v['status'] == '0') {
				$list[$k]['statuss'] = '未开始';
			} else if ($v['status'] == '1') {
				$list[$k]['statuss'] = '进行中';
			} else if ($v['status'] == '2') {
				$list[$k]['statuss'] = '已结束';
			}
			if ($v['category'] == 1) {
				$list[$k]['original_price'] = M('face_course')->where("id=$v[course_id]")->getfield('price');
			} else if ($v['category'] == 2) {
				$list[$k]['original_price'] = M('video_course')->where("id=$v[course_id]")->getfield('price');
			} else {
				$list[$k]['original_price'] = M('live_course')->where("id=$v[course_id]")->getfield('price');
			}
			//参与人数
			$list[$k]['count'] = M('seckill_log')->where("seckill_id=$v[id]")->count();
		}
		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '课程秒杀列表';
		$this->assign('msg', $_REQUEST);
		$this->assign('cur', '课程秒杀');
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 新增秒杀
	 */
	public function addSeckill()
	{
		$iid = session('iid');
		if (IS_POST) {
			// print_r($_REQUEST);die;

			$data['name'] = $_REQUEST['course_name'];

			$data['category'] = $_REQUEST['category'];
			$data['one_type_id'] = $_REQUEST['one_type_id'];
			$data['two_type_id'] = $_REQUEST['two_type_id'];
			$data['course_id'] = $_REQUEST['course_id'];
			$data['start_time'] = strtotime($_REQUEST['start_time']);
			$data['end_time'] = strtotime($_REQUEST['end_time']);
			$data['price'] = $_REQUEST['price'];
			$data['count'] = $_REQUEST['count'];
			$data['surplus_count'] = $_REQUEST['count'];
			//tzb 加个机构ID
			$data['iid'] = $iid;
			if (empty($data['course_id'])) {
				$this->error("请选择课程");
			}

			if (empty($data['start_time'])) {
				$this->error("请选择拼团开始时间");
			}

			if (empty($data['end_time'])) {
				$this->error("请选择拼团结束时间");
			} else {
				if ($data['end_time'] < $data['start_time']) {
					$this->error("拼团结束时间不能小于拼团开始时间");
				} else {
					if ($data['end_time'] < time()) {
						$data['status'] = 2;
					} else {
						if ($data['start_time'] > time()) {
							$data['status'] = 0;
						} else {
							$data['status'] = 1;
						}
					}
				}
			}

			$time = time();
			$data['create_time'] = $time;
			$data['update_time'] = $time;
			$result = D('seckill')->add($data);
			if ($result) {
				//新增成功之后，对应的课程ID is_acticity状态要变
				if ($data['start_time'] <= time()) {
					if ($data['category'] == 1) {
						//面授
						M('face_course')->where("id=$data[course_id]")->save(['is_activity' => 2]);
					} else if ($data['category'] == 2) {
						//点播
						M('video_course')->where("id=$data[course_id]")->save(['is_activity' => 2]);
					} else {
						//直播
						M('live_course')->where("id=$data[course_id]")->save(['is_activity' => 2]);
					}
				} else {
					//活动添加后未开始，添加redis集合，到了对应开始，结束时间就自动执行相关程序
					$redisCtr = A('Home/Redis');
					$task_param = array(
						'seckill_id' => $result,
						'category' => $data['category'],
						'course_id' => $data['course_id']
					);
					//添加开始执行的redis记录到集合
					$redisCtr->addRedis('skillStart', $data['start_time'], $task_param);
					//添加结束执行的redis记录到集合
					$redisCtr->addRedis('skillEnd', $data['end_time'], $task_param);
				}

				if (session('Supertube') == 2) {
					//不记录超管的 只记录这种有权限的
					$data['account_id'] = session('user_id');
					$data['create_time'] = time();
					$data['ip_address'] = $_SERVER["REMOTE_ADDR"];
					$data['content'] = '课程秒杀' . '-' . '新增秒杀';
					M('account_log')->add($data);
				}
				$this->success("新增成功", U('seckill'));
			} else {
				$this->error("新增失败");
			}
		} else {
			//父级分类
			$parent = D('face_course_type')->where(array('pid' => 0))->order('id desc')->select();
			//子级分类
			$child = D('face_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
			//课程
			//$course = D('face_course')->where(array('iid' => $iid, 'one_type_id' => $parent[0]['id'], 'two_type_id' => $child[0]['id']))->order('id desc')->select();
			$course = D('face_course')->where(array('iid' => $iid, 'one_type_id' => $parent[0]['id'], 'two_type_id' => $child[0]['id'], 'is_del' => 0))->order('id desc')->select();
			foreach ($course as $k => $v) {
				$course[$k]['img'] = M('picture')->where("id=$v[img]")->getfield('path');
			}
			$this->assign('parent', $parent);
			$this->assign('child', $child);
			$this->assign('course', $course);
			$this->assign('cur', '课程秒杀');
			$this->display();
		}
	}

	public function seckill_del()
	{
		$id = $_REQUEST['id'];
		$res = M('seckill')->where("id=$id")->delete();
		if ($res) {
			if (session('Supertube') == 2) {
				//不记录超管的 只记录这种有权限的
				$data['account_id'] = session('user_id');
				$data['create_time'] = time();
				$data['ip_address'] = $_SERVER["REMOTE_ADDR"];
				$data['content'] = '课程秒杀' . '-' . '删除';
				M('account_log')->add($data);
			}
			M('seckill_log')->where("seckill_id=$id")->delete();
			$this->success('成功', U('seckill'));
		} else {
			$this->error('失败');
		}
	}

	//tzb 秒杀详情
	public function seckill_details()
	{
		$id = $_REQUEST['id'];
		// print_r($id);die;
		$info = M('seckill')->where("id=$id")->find();
		$course_id = $info['course_id'];
		if ($info['category'] == 1) {
			$info['category_name'] = '面授课';
			$info['g_price'] = M('face_course')->where("id=$course_id")->getfield('price');
			$info['img'] = M('face_course')->where("id=$course_id")->getfield('img');
		} else if ($info['category'] == 2) {
			$info['category_name'] = '点播课';
			$info['g_price'] = M('video_course')->where("id=$course_id")->getfield('price');
			$info['img'] = M('video_course')->where("id=$course_id")->getfield('img');
		} else {
			$info['category_name'] = '直播课';
			$info['g_price'] = M('live_course')->where("id=$course_id")->getfield('price');
			$info['img'] = M('live_course')->where("id=$course_id")->getfield('img');
		}
		if ($info['img']) {
			$info['img'] = M('picture')->where("id=$info[img]")->getField('path');
		} else {
			$info['img'] = '';
		}

		$info['start_time'] = date('Y-m-d H:i:s', $info['start_time']);
		$info['end_time'] = date('Y-m-d H:i:s', $info['end_time']);
		$count = M('seckill_log')->where("seckill_id=$id")->count();
		$Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('seckill_log')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->where("seckill_id=$id")->select();
		$this->assign('count', $count);
		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);
		$this->assign('info', $info);
		$this->assign('cur', '课程秒杀');
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 获取商品
	 */
	public function getGoods()
	{
		$iid = $_REQUEST['iid'];//机构
		$category = $_REQUEST['category'];//类别（1面授，2点播，3直播，4商品）
		if ($category == 1) {
			$list = D('face_course')->field('id,name')->where(array('iid' => $iid))->order('id desc')->select();
		} else if ($category == 2) {
			$list = D('video_course')->field('id,name')->where(array('iid' => $iid))->order('id desc')->select();
		} else if ($category == 3) {
			$list = D('live_course')->field('id,name')->where(array('iid' => $iid))->order('id desc')->select();
		} else {
			$list = D('goods')->field('id,name')->order('id desc')->select();
		}
		$list = $list ? $list : [];

		return api_return(1, '信息获取成功', $list);
	}

	/**
	 * Mr.Jiang
	 * 优惠券列表
	 */
	public function coupon()
	{
		$map['iid'] = session('iid');
		$_REQUEST['name'] ? $map['name'] = array('like', "%$_REQUEST[name]%") : '';
		$_REQUEST['status'] ? $map['status'] = $_REQUEST['status'] : '';
		$count = M('coupon')->count();
		$Page = new \Think\Page($count, 10, $_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('coupon')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->where($map)->select();
		foreach ($list as $k => $v) {
			if ($v['type'] == 1) {
				$list[$k]['type'] = '全部商品可用';
			} else {
				$list[$k]['type'] = '指定商品可用';
			}
			if ($v['status'] == 1) {
				$list[$k]['statuss'] = '已停止发券';
			} else {
				$list[$k]['statuss'] = '进行中';
			}
			//有效期 我想着是这个券的生成日期加上有效日期
			$use_time = $v['use_day'] * 24 * 60 * 60 + $v['create_time'];
			$list[$k]['use_time'] = date('Y-m-d H:i:s', $use_time);

			//已领取多少张优惠券
			$list[$k]['received'] = M('user_coupon')->where("cid=$v[id]")->count();
			$list[$k]['used'] = M('user_coupon')->where("cid=$v[id] && status=1")->count();
		}
		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);
		$this->assign("msg", $_REQUEST);
		$this->meta_title = '优惠券列表';
		$this->assign('cur', '优惠券管理');
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 新增优惠券
	 */
	public function addCoupon()
	{
		$iid = session('iid');
		if (IS_POST) {
			// print_r($_REQUEST);die;
			$data['iid'] = session('iid');
			$data['type'] = $_REQUEST['type'];//tzb 1是全部商品 2是指定商品
			$data['name'] = $_REQUEST['name'];
			$data['goods_ids'] = $_REQUEST['goods_ids'];
			$data['count'] = $_REQUEST['count'];
			$data['get_day'] = $_REQUEST['get_day'];
			$data['min_price'] = $_REQUEST['min_price'];
			$data['deduct_price'] = $_REQUEST['deduct_price'];
			$data['use_day'] = $_REQUEST['use_day'];
			$data['get_count'] = $_REQUEST['get_count'];

			if ($data['name'] == '') {
				$this->error("请输入优惠券名称");
			}

			$time = time();
			$data['update_time'] = $time;
			$data['create_time'] = $time;
			$result = D('coupon')->add($data);
			if ($result) {
				//判断是不指定商品 2是指定商品 才执行下面的代码
				if ($_REQUEST['type'] == 2) {
					$cate_id = $_REQUEST['cate_id'];
					$good_id = $_REQUEST['good_id'];
					foreach ($good_id as $k => $v) {
						$add_data['coupon_id'] = $result;
						$add_data['to_id'] = $v;
						$add_data['type'] = $cate_id[$k];
						M('vice_coupon')->add($add_data);
					}
				} else {
					//全部商品也要添加一条数据到vice_coupon表
					M('vice_coupon')->add(['coupon_id' => $result]);
				}
				if (session('Supertube') == 2) {
					//不记录超管的 只记录这种有权限的
					$data['account_id'] = session('user_id');
					$data['create_time'] = time();
					$data['ip_address'] = $_SERVER["REMOTE_ADDR"];
					$data['content'] = '优惠券管理' . '-' . '新增优惠券';
					M('account_log')->add($data);
				}
				$this->success("新增成功", U('coupon'));
			} else {
				$this->error("新增失败");
			}
		} else {
			//父级分类
			$parent = D('face_course_type')->where(array('pid' => 0))->order('id desc')->select();
			//子级分类
			$child = D('face_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
			//课程
			$course = D('face_course')->where(array('iid' => $iid, 'one_type_id' => $parent[0]['id'], 'two_type_id' => $child[0]['id'], 'is_del' => 0))->order('id desc')->select();
			foreach ($course as $k => $v) {
				$course[$k]['img'] = M('picture')->where("id=$v[img]")->getfield('path');
			}
			$this->assign('parent', $parent);
			$this->assign('child', $child);
			$this->assign('course', $course);
			$this->assign('cur', '优惠券管理');
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 停止发券
	 */
	public function stopCoupons()
	{
		$id = $_REQUEST['id'];
		$coupon = D('coupon')->where(array('id' => $id))->find();
		if ($coupon['is_del'] == 1) {
			$this->error("优惠券已被删除");
		}

		if ($coupon['status'] == 1) {
			$this->error("优惠券已停止发券");
		}

		$expiration_time = $coupon['create_time'] + $coupon['use_day'] * 24 * 60 * 60;
		if ($expiration_time <= time()) {
			$this->error("该优惠券已结束");
		}

		$result = D('coupon')->where(array('id' => $id))->save(array('status' => 1));
		if ($result) {
			if (session('Supertube') == 2) {
				//不记录超管的 只记录这种有权限的
				$data['account_id'] = session('user_id');
				$data['create_time'] = time();
				$data['ip_address'] = $_SERVER["REMOTE_ADDR"];
				$data['content'] = '优惠券管理' . '-' . '停止发券';
				M('account_log')->add($data);
			}
			$this->success("操作成功");
		} else {
			$this->error("操作失败");
		}
	}
}